<?php
class Index extends Controller {

    public $userLog;
    public $userViewId;
    public $userView;
    public $subjects;
    public $introduction;
    public $photos;
    public $numRequest;
    public $profile;
    public $friendRelation;

    public function __construct() {
        parent::__construct();
        $this->_initalize();
    }

    public function __destruct() {
        parent::__destruct();
    }

    public function index() {
	echo md5('van_tai');
        $this->template('index');
    }

    private function _initalize() {
        if (isset($this->_session['user'])) {
            $this->userLog = $this->_session['user']['info'];
        } else {
            header('Location:index.php?c=login');
        }

        $this->model('user');
        $this->user->id = $this->userLog->id;

        //Load library datetimex.
        $this->library('datetimex');
        $numDay = Datetimex::getDaysInMonth(date('m',  strtotime($this->userLog->birthday)), date('Y',  strtotime($this->userLog->birthday)));
        $this->day[-1] = 'Day:';
        for ($i = 1; $i <= $numDay; $i++) {
            $this->day[$i] = $i;
        }

        $this->month = array(
            -1 => 'Month:',
            1 => 'Jan',
            2 => 'Feb',
            3 => 'Mar',
            4 => 'Apr',
            5 => 'May',
            6 => 'Jun',
            7 => 'July',
            8 => 'Aug',
            9 => 'Sep',
            10 => 'Oct',
            11 => 'Nov',
            12 => 'Dec'
        );

        $currentYear = date('Y');
        $this->year[-1] = 'Year:';
        for ($i = $currentYear; $i >= 1905; $i--) {
            $this->year[$i] = $i;
        }

        //Load photo model...
        $this->model('photo');
        $this->userViewId = $this->userLog->id;
        $this->userView = false;
        $this->profile = $this->userLog;
        $this->userViewId = $this->userLog->id;

        if(!empty($this->_get['user'])){
            $this->userView = $this->_get['user'];
            //Process in case user view one user that not existed with the username.
            $userInfo = $this->user->getInfoByUsername($this->userView);
            if(!empty($userInfo)){
                $this->profile = $this->user->getInfoByUsername($this->userView);
                $this->userViewId = $this->profile->id;
            }
        }

        $this->photos = $this->photo->gets($this->userLog->id, $this->userViewId);
        //Load library for word proccessing..
        $this->library('text');
        $this->model('friend');
        $this->numRequest = count($this->friend->getRequest($this->userLog->id));

        //Get friend relation..
        $this->friendRelation = $this->friend->getFriendRelation($this->userLog->id, $this->userViewId);

        //Introduction
        $this->introduction = $this->user->getInfoById($this->userViewId)->introduction;
    }

    public function update() {
        if (isset($this->_post['profile'])) {
            //Check csrf security...
            $token = $this->_post['profile']['token'];
            if ($token == $this->_token) {
                unset($this->_post['profile']['token']);
                $result = $this->user->updateProfile((object) $this->_post['profile']);
                if ((bool) $result['updated']) {
                    if ($result['name'] == 'birthday') {
                        $value = date('m-d-Y', strtotime($result['value']));
                        echo "{result:1, name:'{$result['name']}', value:'". $value ."', txtError:''}";
                        return;
                    }
                    echo "{result:1, name:'{$result['name']}', value:'". $result['value'] ."', txtError:''}";
                } else {
                    echo "{result:0, name:'{$result['name']}', value:'". $result['value'] ."', txtError:'{$result['txt_error']}'}";
                }
            }
        }
    }

    public function introduction() {
        $this->userViewId = $this->userLog->id;
        $this->userView = false;

        if(!empty($this->_get['user'])){
            $this->userView = $this->_get['user'];
            //Process in case user view one user that not existed with the username.
            $userInfo = $this->user->getInfoByUsername($this->userView);
            if(!empty($userInfo)){
                $this->userViewId = $this->user->getInfoByUsername($this->userView)->id;
            }
            else{
                $this->userViewId = $this->userLog->id;
            }
        }

        $this->introduction = $this->user->getInfoById($this->userViewId)
                ->introduction;
        $this->template('introduction');
    }

    public function picture() {
        if (isset($this->_post['photo'])) {
            $this->model('photo');
            $namePhoto = trim($this->_post['photo']['name']);
            //Validation photo...
            $result = $this->photo->add($namePhoto, $this->userLog->id);

            if ((bool) $result['updated']) {
                echo "{result:1, name:'{$namePhoto}', id:{$result['id']}}";
            } else {
                echo "{result:0, name:'{$namePhoto}', txtErr:'{$result['txtErr']}'}";
            }
        }
    }

    public function contact() {
        $this->template('contact');
    }

    public function statitics() {
        $this->template('statictics');
    }

    public function delphoto() {
        if (isset($this->_post['photo'])) {
            //Check csrf security...
            $token = $this->_post['photo']['token'];
            if ($token == $this->_token) {
                unset($this->_post['photo']['token']);
                $idPhoto = $this->_post['photo']['id'];
                $result = $this->photo->delete($idPhoto);

                if ((bool) $result) {
                    echo "{result:1, id:'{$idPhoto}'}";
                } else {
                    echo "{result:0, id:'{$idPhoto}'}";
                }
            }
        }
    }

    public function likephoto() {
        if (isset($this->_post['photo'])) {
            //Check csrf security...
            $token = $this->_post['photo']['token'];
            if ($token == $this->_token) {
                unset($this->_post['photo']['token']);
                $idPhoto = $this->_post['photo']['id'];
                $result = $this->photo->like($idPhoto, $this->userLog->id);

                if ((bool) $result) {
                    echo "{result:1, numLike:'{$result}', id:{$idPhoto}}";
                } else {
                    echo "{result:0, numLike:'{$result}, id:{$idPhoto}}";
                }
            }
        }
    }

    public function viewphoto() {
        if (isset($this->_post['photo'])) {
            //Check csrf security...
            $token = $this->_post['photo']['token'];
            if ($token == $this->_token) {
                unset($this->_post['photo']['token']);
                $idPhoto = $this->_post['photo']['id'];
                //Get num view photo....
                $result = $this->photo->view($idPhoto, $this->userLog->id);

                if ((bool) $result) {
                    echo "{result:1, id:'{$idPhoto}', numView:{$result}}";
                } else {
                    echo "{result:1, id:'{$idPhoto}', numView:{$result}}";
                }
            }
        }
    }

    public function updatelocation() {
        if (isset($this->_post['location'])) {
            //Check csrf security...
            $token = $this->_post['location']['token'];
            if ($token == $this->_token) {
                unset($this->_post['location']['token']);
                $location_x = $this->_post['location']['loca_x'];
                $location_y = $this->_post['location']['loca_y'];
                if(is_null($location_y) || is_null($location_x)
                        || !is_numeric($location_y) || !is_numeric($location_x)){
                    echo "{result:0, txtErr: 'Location can not empty and is numeric'}";
                    return;
                }
                //Get num view photo....
                $result = $this->user->updateLocation($this->userLog->id, implode('|', array($location_x, $location_y)));

                if ((bool) $result) {
                    echo "{result:1, loca_x:'{$location_x}', loca_y:{$location_y}}";
                } else {
                    echo "{result:0, loca_x:'{$location_x}', loca_y:{$location_y}}";
                }
            }
        }
    }

    public function addfriend() {
        if (isset($this->_post['friend'])) {
            //Check csrf security...
            $token = $this->_post['friend']['token'];
            if ($token == $this->_token) {
                unset($this->_post['friend']['token']);
                $idFriend = $this->_post['friend']['id'];
                //Get num view photo....
                //Load model friend..
                $this->model('friend');
                $result = $this->friend->add($this->userLog->id, $idFriend);

                if ((bool) $result) {
                    echo "{result:1, id:'{$idFriend}', idself:{$this->userLog->id}}";
                } else {
                    echo "{result:1, id:'{$idFriend}', idself:{$this->userLog->id}}";
                }
            }
        }
    }

    public function updateprofile() {
        if (isset($this->_post['profile'])) {
            $this->user->id = $this->userLog->id;
            $result = $this->user->updateAvatar();

            if ((bool) $result['updated']) {
                echo "{result:1, src:'{$result['profile']}'}";
            } else {
                echo "{result:0, txtErr:'{$result['txtErr']}'}";
            }
        }
    }

}

?>
